module V1
  class Users < Grape::API
    # GET /v1/
    params do
      requires :name, type: String
      requires :password, type: String
    end
    post 'admin_user/create' do
      error "此账号已存在" if AdminUser.find_by(name:params[:name])
      user = AdminUser.create(name: params[:name], password: params[:password], password_confirmation: params[:password])
      token = user.try(:token)
      render_json user:user,token:token[:token]
    end

    post 'admin_user/login' do
      user = AdminUser.find_by(name: params[:name]).try(:authenticate, params[:password])
      if token = user.try(:token)
        render_json user:user,token:token[:token]
      else
        error "账号密码错误"
      end
    end
  end
end
